//
//  ScrollImageView.m
//  滚动视图封装
//
//  Created by dlios on 15/6/12.
//  Copyright (c) 2015年 SJF1992. All rights reserved.
//

#import "ScrollImageView.h"
#import "HomePageModel.h"
#import "UIButton+WebCache.h"
#import "BaseHeader.h"
@implementation ScrollImageView

- (instancetype)initWithFrame:(CGRect)frame withImageArray:(NSMutableArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatViewWithArry:array];
    }

    return self;
}


- (void)creatViewWithArry:(NSMutableArray *)array
{
    // 将外界传进来的数组处理,前后各加一张
    self.imageArr = [NSMutableArray arrayWithArray:array];
    NSString *first = [self.imageArr firstObject];
    NSString *last = [self.imageArr lastObject];
    [self.imageArr insertObject:last atIndex:0];
    [self.imageArr addObject:first];
    self.myScroll = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.myScroll.pagingEnabled = YES;
    self.myScroll.contentSize = CGSizeMake(self.imageArr.count * self.frame.size.width,self.frame.size.height);
    self.myScroll.bounces = NO;
    self.myScroll.showsHorizontalScrollIndicator = NO;
    self.myScroll.contentOffset = CGPointMake(self.myScroll.frame.size.width, 0);
    self.myScroll.delegate = self;
    [self.myScroll release];
    [self addSubview:self.myScroll];

    // 写一个modle类创建一个字符串属性用来存放相片的名字 如果有点击要求 这里的可以button
    for (int i = 0; i < _imageArr.count; i ++) {

        HomePageModel *modle = [[[HomePageModel alloc] init] autorelease];
        modle = [_imageArr objectAtIndex:i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * _myScroll.frame.size.width, 0, _myScroll.frame.size.width, _myScroll.frame.size.height);
        [button setBackgroundImageWithURL:[NSURL URLWithString:modle.img] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"placeholder"]];
        UIView *viewText = [[[UIView alloc] initWithFrame:CGRectMake(80, 140, 175, 22)] autorelease];
        viewText.backgroundColor = [UIColor blackColor];
        viewText.alpha = 0.6;
        [viewText.layer setCornerRadius:10];
        [button addSubview:viewText];
        UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, viewText.frame.size.width - 6, viewText.frame.size.height)] autorelease];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.text = modle.title;
        lbl.font = [UIFont boldSystemFontOfSize:17];
        lbl.textColor = [UIColor whiteColor];
        [viewText addSubview:lbl];
        [button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 2000 + i;
        [self.myScroll addSubview:button];
    }

    self.myPage = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width/2 - 100, self.frame.size.height - 30, 200, 20)];
    self.myPage.numberOfPages = _imageArr.count - 2;
    self.myPage.pageIndicatorTintColor = [UIColor whiteColor];
    self.myPage.currentPageIndicatorTintColor = REDCOLOR(1);
    [self.myPage addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.myPage];
    [self.myPage release];
    // 定时滚动方法
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(runTime) userInfo:nil repeats:YES];
}
// 实现循环滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    if (scrollView.contentOffset.x == _myScroll.frame.size.width * (_imageArr.count - 1)) {
        [_myScroll setContentOffset:CGPointMake(_myScroll.frame.size.width, 0) animated:NO];
    }
    if (scrollView.contentOffset.x == 0) {
        [_myScroll setContentOffset:CGPointMake(self.myScroll.frame.size.width * (_imageArr.count - 2), 0) animated:NO];
    }

    _myPage.currentPage = (_myScroll.contentOffset.x / self.myScroll.frame.size.width) - 1;
}

// pageControl的值改变 会带动视图
- (void)changePage:(UIPageControl *)page
{
    [_myScroll setContentOffset:CGPointMake((page.currentPage + 1) * self.myScroll.frame.size.width, 0) animated:NO];
}

// 定时器方法
- (void)runTime
{

    NSInteger page = self.myPage.currentPage;
    page ++;
    page = page > self.imageArr.count - 3 ? 0 : page;
    self.myPage.currentPage = page;
    [self changePage:self.myPage];
    [self scrollViewDidEndDecelerating:self.myScroll];
}

- (void)clicked:(UIButton *)button
{

    for (int i = 0; i < _imageArr.count; i ++) {
        if (button.tag == 2000 + i) {
            HomePageModel *model = [[HomePageModel alloc] init];
            model = [_imageArr objectAtIndex:i];
            [self.delegate pushToNextControllerWithIDName:model.type];

        }
    }
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
